package br.com.felix.fwt.ui;

import java.io.PrintWriter;

import org.apache.log4j.Logger;

import br.com.felix.fwt.log.LoggerFactory;
import br.com.felix.fwt.ui.css.Style;
import br.com.felix.fwt.ui.exception.ComponentRenderException;
import br.com.felix.fwt.ui.exception.FontUnavailableException;
import br.com.felix.fwt.ui.image.TextImageBuilder;
import br.com.felix.fwt.util.exception.ImageGenerationException;
/**
 * This class allows you to create dynamic images.
 * This way you can use any Font you want.
 * */
public class TextImage extends Image{


	private static final long serialVersionUID = -3599145385102447247L;
	
	private static Logger logger = LoggerFactory.getLogger(TextImageButton.class);
	
	private TextImageBuilder imageBuilder;
	
	/**
	 * Flag that indicates whether the image needs to be recreated.
	 * */
	private boolean recreateImage;
	
	/**
	 * Used to configure the Font used to create the text image.
	 * */
	private Style style;


	
	/**
	 * @param value the text to be displayed by the image.
	 * @throws FontUnavailableException 
	 * */
	public TextImage(String value) throws FontUnavailableException{
	    this(value, null, new TextImageBuilder());
	}
	
	
	/**
     * @param value the text to be displayed by the image.
     * @param style the font style.
	 * @throws FontUnavailableException 
     * */
    public TextImage(String value, Style style) throws FontUnavailableException{
        this(value, style, new TextImageBuilder());
    }
	
    /**
     * @param value the text to be displayed by the image.
     * @param style the font style.
     * @throws FontUnavailableException 
     * */
    public TextImage(String value, Style style, TextImageBuilder builder) throws FontUnavailableException{
        imageBuilder = builder;
        imageBuilder.setText(value);
        imageBuilder.setStyle(style);
        recreateImage = imageBuilder.needsToBeCreated();
        setValue(imageBuilder.getWebFilePath());
        setStyle(style);
    }
	
	/**
	 * @param style the style used to configure the Font of the text image.
	 * */
	@Override
	public void setStyle(Style style) {
	    if (style!=null){
	        this.style = (Style)style.clone();
	    }
	}

	
	
	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		if (recreateImage){
			try {
                createImage();
            } 
			catch (ImageGenerationException e) {
                String msg = "Could not render TextImage. Error generating image.";
                logger.error(msg, e);
                throw new ComponentRenderException(msg, e);
            } 
			catch (FontUnavailableException e) {
			    String msg = "Could not render TextImage. Chosen font is not available.";
                logger.error(msg, e);
                throw new ComponentRenderException(msg, e);
            }
		}
		super.write(out);
	}

	
	
	/**
	 * Creates a PNG image with the text, font and style provided.
	 * @throws ImageGenerationException 
	 * @throws FontUnavailableException 
	 * 
	 * */
	private void createImage() throws ImageGenerationException, FontUnavailableException {
		logger.info("Creating image for button '"+getValue()+"'");
		imageBuilder.makeImage();
	}



	/**
	 * Forces image generation.
	 * */
    public void recreateImage() {
        this.recreateImage = true;
    }
}
